package com.taotao.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.enums.GoodsStatus;
import com.taotao.common.enums.OperateType;
import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.Constants;
import com.taotao.common.utils.DateUtil;
import com.taotao.mapper.EyMemberMapper;
import com.taotao.mapper.EyTicketMapper;
import com.taotao.pojo.EyGoods;
import com.taotao.pojo.EyMember;
import com.taotao.pojo.EyTicket;
import com.taotao.pojo.EyTicketExample;
import com.taotao.service.OperateLogService;
import com.taotao.service.TicketService;
import com.taotao.service.WechatService;

@Service
public class TicketServiceImpl implements TicketService {

	@Autowired
	EyTicketMapper eyTicketMapper;
	@Autowired
	WechatService wechatService;
	@Autowired
	EyMemberMapper eyMemberMapper;
	@Autowired
	OperateLogService operateLogService;
	
	BaseResult baseResult = new BaseResult();
	

	@Override
	public Map<String, Object> getTicketList(Map<String, String> map) {
		try {
			String pageNum=map.get("pageNum");
			String pageSize=map.get("pageSize");
			String memberId=map.get("memberId");
			String currentTab=map.get("currentTab"); 
			Integer num=StringUtils.isBlank(pageNum)?Constants.pageNum:Integer.parseInt(pageNum);
			Integer size=StringUtils.isBlank(pageSize)?Constants.pageSize:Integer.parseInt(pageSize);
			
			EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
			if(member==null){
				return baseResult.getErrorMap("会员编号不存在,memberId:"+memberId);
			}

			//如果设置了自己的头像，则使用自己的头像，
			if(StringUtils.isNotBlank(member.getImageUrl())){
				member.setAvatarUrl(member.getImageUrl());
			}
			
			EyTicketExample example = new EyTicketExample();
			com.taotao.pojo.EyTicketExample.Criteria criteria = example.createCriteria();
			if(!Constants.num_0.equals(currentTab)){ //0 是查询全部
				criteria.andStatusEqualTo(currentTab);
			}
			criteria.andMemberIdEqualTo(Integer.parseInt(memberId));
			example.setOrderByClause(" effect_date desc ");
			PageHelper.startPage(num,size);
			List<EyTicket> list = eyTicketMapper.selectByExample(example);
			
			PageInfo<EyTicket> pageInfo = new PageInfo<>(list);
	  		return baseResult.getSuccMap(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}


	@Override
	public Map<String, Object> modifyTicketStatus(Map<String, String> map) {
		try {
			String ticketId=map.get("ticketId");
			String memberId=map.get("memberId");
			String status=map.get("status");
			
			EyTicket ticket = eyTicketMapper.selectByPrimaryKey(Integer.parseInt(ticketId));
			if(ticket==null){
				return baseResult.getErrorMap("ticketId不存在,ticketId:"+ticketId);
			}
			EyTicket t=new EyTicket();
			t.setId(Integer.parseInt(ticketId));
			t.setStatus(status);
			t.setUpdateTime(DateUtil.getDateAndTime());
			eyTicketMapper.updateByPrimaryKeySelective(t);
			
			String operateType=OperateType.operateType_38.getCode();
			
			operateLogService.insert(Integer.parseInt(memberId), null,null,"修改ticket状态，状态改为："+status,operateType,""); 
			
			return baseResult.getSuccMap();
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	

}
